﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.ProcessExecutor.Core.Engine;
using DWF.Studio.ProcessExecutor.Core.Process;
using DWF.Studio.ProcessExecutor.Core.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.ComponentModel;

namespace DWF.Studio.ProcessExecutor.Core
{
    public static class EngineLaucher
    {
        static EngineLaucher()
        {
            AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
                {
                    LogHelper.Error(e.ExceptionObject);
                };
        }

        public static void Launch()
        {
            var processDirectory = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Process");

            var processResolver = new ProcessResolver();
            var processInfo = processResolver.Resolve(processDirectory);

            var engine = new WorkflowEngine();
            engine.ExecuteProcess(processInfo);
            killProcess();
        }
        private static void killProcess()
        {
            foreach (string processName in processNames())
            {
                foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
                {
                    if (p.ProcessName.Contains(processName))
                    {
                        try
                        {
                            p.Kill();
                            p.WaitForExit(); // possibly with a timeout
                            Console.WriteLine($"进程结束关闭{processName}进程！！！");
                        }
                        catch (Win32Exception e)
                        {
                            Console.WriteLine(e.Message.ToString());
                            if (Share.Log.SystemEventLog.CreateSystemEventLogCategory("Application", "ProcessGo"))
                            {
                                Share.Log.SystemEventLog.WriteSystemEventLog("Application", e.Message.ToString(), EventLogEntryType.Error);
                            }
                            
                        }
                        catch (InvalidOperationException e)
                        {
                            Console.WriteLine(e.Message.ToString());
                            if (Share.Log.SystemEventLog.CreateSystemEventLogCategory("Application", "ProcessGo"))
                            {
                                Share.Log.SystemEventLog.WriteSystemEventLog("Application", e.Message.ToString(), EventLogEntryType.Error);
                            }
                        }
                    }
                }
            }
        }
        private static List<string> processNames()
        {
            return new List<string>()
            {
                "chromedriver"
            };
        }
    }
}
